﻿using System;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;

namespace YanForum.Engine.Services
{
    //By Tuan
    public static class DateExtension
    {
        public static string TimeAgo(this DateTime t)
        {
            TimeSpan timeSince = DateTime.Now.Subtract(t);

            if (timeSince.TotalMilliseconds < 1)
                return "chưa có";
            if (timeSince.TotalSeconds < 60)
                return string.Format("Đăng cách đây {0} giây", timeSince.Seconds);
            if (timeSince.TotalMinutes < 60)
                return string.Format("Đăng cách đây {0} phút", timeSince.Minutes);
            if (timeSince.TotalMinutes < 120)
                return "Đăng cách đây 1 giờ";
            if (timeSince.TotalHours < 24)
                return string.Format("Đăng cách đây {0} giờ", timeSince.Hours);
            if (timeSince.TotalDays >= 1 && timeSince.TotalDays < 2)
                return "hôm qua";
            if (timeSince.TotalDays < 7)
                return string.Format("Đăng cách đây {0} ngày", timeSince.Days);
            if (timeSince.TotalDays < 14)
                return "Đăng cách đây 1 tuần";
            if (timeSince.TotalDays < 21)
                return "Đăng cách đây 2 tuần";
            if (timeSince.TotalDays < 28)
                return "Đăng cách đây 3 tuần";
            if (timeSince.TotalDays < 60)
                return "Đăng cách đây 1 tháng";
            if (timeSince.TotalDays < 365)
                return string.Format("Đăng cách đây {0} tháng", Math.Round(timeSince.TotalDays / 30));
            if (timeSince.TotalDays < 730)
                return "Đăng cách đây 1 năm";
            //last but not least...
            return string.Format("Đăng cách đây {0} năm", Math.Round(timeSince.TotalDays / 365));
        }

        //public static string DateTimeFormat(this HtmlHelper helper, DateTime date)
        //{
        //    return
        //        string.Format(ConfigurationManager.AppSettings["DateTimeFormat"], date)
        //            .Replace("SA", "sáng")
        //            .Replace("CH", "chiều");
        //}

        #region Today

        public static DateTime GetStartOfToday()
        {
            DateTime dt = DateTime.Today;
            return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, 0);
        }

        public static DateTime GetEndOfToday()
        {
            DateTime dt = DateTime.Today;
            return new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59, 999);
        }

        #endregion

        #region Date

        public static DateTime GetStartOfDate(DateTime d)
        {
            return new DateTime(d.Year, d.Month, d.Day, 0, 0, 0, 0);
        }

        public static DateTime GetEndOfDate(DateTime d)
        {
            return new DateTime(d.Year, d.Month, d.Day, 23, 59, 59, 999);
        }

        #endregion

        #region Weeks

        public static DateTime GetStartOfLastWeek()
        {
            //int DaysToSubtract = (int)DateTime.Now.DayOfWeek + 7;
            //DateTime dt = DateTime.Now.Subtract(TimeSpan.FromDays(DaysToSubtract));
            DateTime dt = GetStartOfCurrentWeek().AddDays(-7);
            return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, 0);
        }

        public static DateTime GetEndOfLastWeek()
        {
            DateTime dt = GetStartOfCurrentWeek().AddDays(-1);
            return new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59, 999);
        }

        public static DateTime GetStartOfCurrentWeek()
        {
            var daysToSubtract = (int)DateTime.Now.DayOfWeek;
            DateTime dt = DateTime.Now.Subtract(TimeSpan.FromDays(daysToSubtract));
            return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, 0);
        }

        public static DateTime GetEndOfCurrentWeek()
        {
            DateTime dt = GetStartOfCurrentWeek().AddDays(6);
            return new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59, 999);
        }

        #endregion

        #region Months

        public static DateTime GetStartOfLastMonth()
        {
            DateTime dt = GetStartOfCurrentMonth().AddMonths(-1);
            return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, 0);
        }

        public static DateTime GetEndOfLastMonth()
        {
            DateTime dt = GetStartOfCurrentMonth().AddDays(-1);
            return new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59, 999);
        }

        public static DateTime GetStartOfCurrentMonth()
        {
            return new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 0, 0, 0, 0);
        }

        public static DateTime GetEndOfCurrentMonth()
        {
            DateTime dt = GetStartOfCurrentMonth().AddMonths(1).AddDays(-1);
            return new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59, 999);
        }

        public static DateTime GetStartOfMonth(DateTime dt)
        {
            return new DateTime(dt.Year, dt.Month, 1, 0, 0, 0, 0);
        }

        public static DateTime GetEndOfMonth(DateTime dt)
        {
            DateTime d = GetStartOfMonth(dt).AddMonths(1).AddDays(-1);
            return new DateTime(d.Year, d.Month, d.Day, 23, 59, 59, 999);
        }

        #endregion
    }

    public static class StringExtension
    {
        public static string ToSlug(this string source)
        {
            return ToSlug(source, int.MaxValue);
        }

        public static string ToSlug(this string source, int maxLength)
        {
            string str = RemoveSign4VietnameseString(source.ToLower());

            // invalid chars, make into spaces
            str = Regex.Replace(str, @"[^a-z0-9\s-]", "");
            // convert multiple spaces/hyphens into one space       
            str = Regex.Replace(str, @"[\s-]+", " ").Trim();
            // cut and trim it
            str = str.Substring(0, str.Length <= maxLength ? str.Length : maxLength).Trim();
            // hyphens
            str = Regex.Replace(str, @"\s", "-");

            return str;
        }

        public static string RemoveSign4VietnameseString(this string source)
        {
            string[] vietnameseSigns =
            {
                "aAeEoOuUiIdDyY", "áàạảãâấầậẩẫăắằặẳẵ", "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ", "éèẹẻẽêếềệểễ",
                "ÉÈẸẺẼÊẾỀỆỂỄ", "óòọỏõôốồộổỗơớờợởỡ", "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ", "úùụủũưứừựửữ", "ÚÙỤỦŨƯỨỪỰỬỮ", "íìịỉĩ", "ÍÌỊỈĨ",
                "đ", "Đ", "ýỳỵỷỹ", "ÝỲỴỶỸ"
            };

            //Tiến hành thay thế , lọc bỏ dấu cho chuỗi
            for (int i = 1; i < vietnameseSigns.Length; i++)
            {
                for (int j = 0; j < vietnameseSigns[i].Length; j++)
                    source = source.Replace(vietnameseSigns[i][j], vietnameseSigns[0][i - 1]);
            }

            return source;
        }

        private static Random random = new Random((int)DateTime.Now.Ticks);
        public static string RandomString(int size)
        {
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }

            return builder.ToString();
        }

        public static bool ValidateEmail(string email)
        {
            try
            {
                MailAddress m = new MailAddress(email);

                return true;
            }
            catch (FormatException)
            {
                return false;
            }
        }
    }

}
